﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using ChuangDu_Spirit_QGB_DAL;
using ChuangDu_Spirit_QGB_IDAL;
namespace ChuangDu_Spirit_QGB_DALContainer
{
    public class DALContainer
    {   /// <summary>
        /// IOC容器
        /// </summary>
        public static  IContainer  container = null;
        /// <summary>
        ///获取 IDal 的实例化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IT Resolve<T, IT>()
        {
            try
            {
                container = null;
                if (container == null)
                {
                    Initialise<T, IT>();

                }
            }
            catch (System.Exception ex)
            {
                throw new System.Exception("IOC实列化错误！" + ex.Message);

            }
            return container.Resolve<IT>();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public static void Initialise<T, IT>()
        {
            var builder = new ContainerBuilder();
            //格式：builder.RegisterType<xxxx>().As<Ixxxx>().InstancePerLifetimeScope();
            builder.RegisterType<T>().As<IT>().InstancePerLifetimeScope(); 
             container =builder.Build();
        }
    }
}
